/**
 * 签到逻辑
 */
class SignUI extends UIBase{
    private btn_close:BaseButton;
    private btn_sign:BaseButton;
    private round_1:eui.Group;
    private round_2:eui.Group;
    private btn_double:BaseButton;
    private btn_get:eui.Label;
    private seven_di:eui.Image;
    private img_icon_1:eui.Image;
    private img_icon_2:eui.Image;
    private img_day:eui.Image;
    private seven_group:eui.Group;
    private title_day:eui.Image;
    private img_got:eui.Image;
    private item_group:eui.Group;
    private title_word:eui.Image;
    private img_mask:eui.Image;

    private _data:any;

    constructor()
    {
        super('SignView',{showBG:true});
    }

    public init()
    {
        super.init(true);
        this.requsetSignInfo();
    }

    public requsetSignInfo()
    {
        let data = {
            version:GameConst.version
        }
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorother/sign_in_info', StringUtil.jsonToHttpPostString(data), (e)=>{
            let data = JSON.parse(e);
            if(data && data.data)
            {
                this.initUI(data.data);
            }
        })
    }

    public initUI(data)
    {
        let sign_data = JSON.parse(JSON.stringify(DataJson.sign_data));
        this.data = sign_data;
        let round = data.sign_in_round;
        let days = data.sign_in_num;
        let is_sign_in = data.is_sign_in;
        RoleCache.instance().signDays = days;
        let isSeven = days == 7;
        this.round_1.visible = !isSeven;
        this.round_2.visible = isSeven;
        if(is_sign_in)
        {
            let img_btn:eui.Image;
            if(isSeven)
                img_btn = this.btn_sign.getChildByName('img_btn') as eui.Image;
            else
                img_btn = this.btn_double.getChildByName('img_btn') as eui.Image;
            if(img_btn)
            {
                img_btn.source = 'btn_signd';
                ShaderUtil.ImgGray(img_btn);
            }
        }
        this._data = data;
        for(let k in sign_data)
        {
            let id = parseInt(k);
            if(id < 7)
            {
                let v = sign_data[k];
                v.days = days;
                v.round = round;
                v.isSign = is_sign_in;
                let item:SignItem = new SignItem(v);
                this.item_group.addChild(item);
                item.x = 21 + Math.floor((id - 1) % 3) * (item.width + 21);
                item.y = 21 + Math.floor(id / 4) * (item.height + 21);
            }
        }
        this.initSevenDay(data);
    }

    public addEvent()
    {
        this.btn_close.addTapHandler(this.close, this);
        this.btn_sign.addTapHandler(this.getSingleAward, this);
        this.btn_double.addTapHandler(this.getMoreAward,this);
        this.btn_get.addEventListener(egret.TouchEvent.TOUCH_TAP,this.getSingleAward,this);
    }

    public initSevenDay(data)
    {
        let round = data.sign_in_round;
        let days = data.sign_in_num;
        let is_sign_in = data.is_sign_in;
        let isSeven = days == 7;
        if(round == 1)
        {
            //第一轮
            this.title_word.source = 'title_seven_2';
            this.img_icon_1.visible = false;
            this.img_icon_2.visible = true;
        }
        else
        {
            //第二轮
            this.title_word.source = 'title_seven_1';
            this.img_icon_1.visible = true;
            this.img_icon_2.visible = false;
        }
        if(isSeven)
        {
            this.seven_di.source = 'seven_di_2';
            this.img_day.visible = false;
            this.title_day.visible = true;
            if(is_sign_in)
            {
                //签到过了
                this.img_got.visible = true;
                // this.seven_group.visible = true;
                // this.title_day.visible = false;
                this.img_mask.visible = true;
            }
            else
            {
                //未签到
                // this.title_day.visible = true;
            }
        }
    }

    public getSingleAward()
    {
        this.sign(1);//单倍领取
    }

    public getMoreAward()
    {
        if(this._data.is_sign_in)
        {
            MessageUtil.tips('已经签到过了哦~');
            return;
        }
        if(GlobalManager.instance().isWindows())
        {
            this.sign(2);
            return ;
        }
        let self = this;
        let Success_call = function()
        {  
            self.sign(2);//双倍领取
        };
        let Error_call = function()
        {  
            MessageUtil.tips('观看完整视频才能双倍领取哦');
        };
        WeChatgGameManager.instance().showVideoAd(GameConst.adList.other_vedio, Success_call, Error_call, this);
    }

    private sign(type:number)
    {
        if(this._data.is_sign_in)
        {
            MessageUtil.tips('已经签到过了哦~');
        }
        else
        {
            this.signHandle(type);
        }
    }

    public signHandle(type:number)
    {
        let data = {
            version:GameConst.version,
            multiple:type
        }
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorother/sign_in', StringUtil.jsonToHttpPostString(data), (e)=>{
            let data = JSON.parse(e);
            if(data.msg == 'ok')
            {
                let award = data.data.award;
                this._data.is_sign_in = true;
                GameUtil.openAwardUI(award, null);
                MessageUtil.tips('签到成功');
                RoleCache.instance().isSign = true;
                GameUtil.updateRedPoint(RedPointID.Sign);
                this.close();
            }
            else
            {
                MessageUtil.tips('签到失败');
            }
        })
    }

    public close()
    {
        this.remove(1);
    }

    public removeEvent()
    {
        this.btn_close.removeTapHandler();
        this.btn_sign.removeTapHandler();
        this.btn_double.removeTapHandler();
        this.btn_get.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.getSingleAward,this);
    }

    public clear()
    {
        super.clear();
    }
}